package com.mspbots.attendance.controller;


import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mspbots.attendance.model.TenantAttendance;
import com.mspbots.attendance.service.TenantAttendanceService;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

@Log4j2
@RestController
@RequestMapping("tenantattendances")
public class TenantAttendanceController {

    private final TenantAttendanceService tenantAttendanceService;

    private TenantAttendanceController(TenantAttendanceService tenantAttendanceService) {
        this.tenantAttendanceService = tenantAttendanceService;
    }


    @GetMapping("log")
    public Callable log(TenantAttendance tenantAttendance) {
        return () -> tenantAttendanceService.log(tenantAttendance);
    }

    @GetMapping("findTeam")
    public Callable findTeam(TenantAttendance tenantAttendance) {
        return () -> tenantAttendanceService.findTeam(tenantAttendance);
    }


    @GetMapping("findCurrentDayLog")
    public Callable findCurrentDayLog(TenantAttendance tenantAttendance) {
        log.debug("findCurrentDayLog:::getTenantId:::" + tenantAttendance.getTenantId());
        log.debug("findCurrentDayLog::getUserId::" + tenantAttendance.getUserId());
        return () -> tenantAttendanceService.findCurrentDayLog(tenantAttendance);
    }

    @GetMapping("findInLog")
    public Callable findInLog(TenantAttendance tenantAttendance) {
        return () -> tenantAttendanceService.findInLog(tenantAttendance);
    }

    @GetMapping("findByParam")
    public Callable findByParam(Page<TenantAttendance> page, TenantAttendanceParams params) {
        return () -> tenantAttendanceService.findByParam(page, params);
    }

    @GetMapping("findTenantLog")
    public Callable findTenantLog(TenantAttendanceParams params) {
        return () -> tenantAttendanceService.findTenantLog(params);
    }

    @GetMapping("findTenantUser")
    public Callable findTenantUser(TenantAttendanceParams params) {
        return () -> tenantAttendanceService.findTenantUser(params);
    }

    @GetMapping("findExecl")
    public Callable findExecl(TenantAttendanceParams params, HttpServletResponse response) {
        return () -> tenantAttendanceService.findExecl(params, response);
    }


    @GetMapping("healthcheck")
    public Callable healthcheck() {
        return () -> true;
    }


    @GetMapping("commandRegix")
    public Callable commandRegix(String regesStr) {
        return () -> tenantAttendanceService.commandRegix(regesStr);
    }

    @GetMapping("findAction")
    public Callable findAction(TenantAttendanceParams params) {
        return () -> tenantAttendanceService.findAction(params);
    }

    @GetMapping("findLastIn")
    public Callable findLastIn(Long tenantId) {
        return () -> tenantAttendanceService.findLastIn(tenantId);
    }

    @GetMapping("findSubAction")
    public Callable findSubAction(Long tenantId) {
        return () -> tenantAttendanceService.findSubAction(tenantId);
    }

    @Data
    @NoArgsConstructor
    public static class TenantAttendanceParams {
        private LocalDateTime createTimeStart;
        private LocalDateTime createTimeEnd;
        private String team;
        private Long userId;
        private String userIds;
        private String action;
        private String subAction;
        private Long tenantId;

        public Map<SFunction<TenantAttendance, ?>, Object> toMap() {
            Map<SFunction<TenantAttendance, ?>, Object> resultMap = new HashMap<>(8);
            resultMap.put(TenantAttendance::getTenantId, this.tenantId);
            resultMap.put(TenantAttendance::getUserId, this.userId);
//            resultMap.put(TenantAttendance::getTeamsUserId, this.teamsId);
            resultMap.put(TenantAttendance::getAction, this.action);
            resultMap.put(TenantAttendance::getSubAction, this.subAction);
            return resultMap;
        }
    }

}
